package com.syp.hehe.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.syp.hehe.dao.CommentMapper;
import com.syp.hehe.domain.RecordStatusEnum;
import com.syp.hehe.domain.pojo.CommentSchema;
import com.syp.hehe.domain.vo.CommentVo;
import com.syp.hehe.service.CommentServiceI;
import com.syp.hehe.util.SessionInfo;
import com.syp.hehe.util.paging.Page;
import com.syp.hehe.util.paging.PageOffSet;

@Service("commentService")
public class CommentServiceImpl implements CommentServiceI {
	
	@Autowired
	private CommentMapper commentMapper;

	@Override
	public void insert(CommentVo comment) {
		CommentSchema commentSchema = new CommentSchema();

		commentSchema.setUuid(comment.getUuid());
		commentSchema.setContent(comment.getContent().replace("\r\n", "<br/>").replace("\n", "<br/>"));
		commentSchema.setArticleId(comment.getArticleId());
		
		HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
		
		commentSchema.setWriterUsername(sessionInfo.getUsername());
		commentSchema.setCreateTime(Calendar.getInstance().getTime());
		commentSchema.setStatus(RecordStatusEnum.NORMAL.getStatus());

		this.commentMapper.insert(commentSchema);
	}

	@Override
	public CommentVo getByUUID(String uuid) {
		CommentSchema commentSchema = this.commentMapper.getByUUID(uuid);
		
		CommentVo comment = new CommentVo();
		comment.setId(commentSchema.getId());
		comment.setUuid(commentSchema.getUuid());
		comment.setContent(commentSchema.getContent());
		comment.setArticleId(commentSchema.getArticleId());
		comment.setCreateTime(commentSchema.getCreateTime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		comment.setCreateTimeText(df.format(commentSchema.getCreateTime()));
		comment.setWriterUsername(commentSchema.getWriterUsername());
		return comment;
	}

	@Override
	public Page<CommentVo> getPageList(Long articleId, Long pageNum, Integer pageSize) {
		Page<CommentVo> page = new Page<CommentVo>();
		List<CommentVo> list = new LinkedList<CommentVo>();
		Long beginIndex = (pageNum - 1) * pageSize;
		List<CommentSchema> commentSchemaList = this.commentMapper.getPageList(articleId, beginIndex, pageSize);
		for (CommentSchema item : commentSchemaList) {
			CommentVo comment = new CommentVo();
			
			comment.setId(item.getId());
			comment.setUuid(item.getUuid());
			comment.setContent(item.getContent());
			comment.setArticleId(item.getArticleId());
			comment.setCreateTime(item.getCreateTime());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			comment.setCreateTimeText(df.format(item.getCreateTime()));
			comment.setWriterUsername(item.getWriterUsername());

			list.add(comment);
		}
		page.setPageIndex(pageNum);
		page.setPageSize(pageSize);
		page.setDatas(list);
		
		Boolean hasMoreRow = false;
		if(page.getDatas().size() < pageSize){
			hasMoreRow = false;
		} else if(page.getDatas().size() == pageSize){
			hasMoreRow = this.commentMapper.hasMorePage(articleId, beginIndex + pageSize);
		}
		if(list.size() > 0){
			page.setMaxId(list.get(0).getId());
			page.setMinId(list.get(list.size()-1).getId());
		}
		page.setHasNextPage(hasMoreRow);
		return page;
	}

	@Override
	public PageOffSet<CommentVo> getPageOffSetList(Long articleId, Long referId, Integer pageSize) {
		PageOffSet<CommentVo> page = new PageOffSet<CommentVo>();
		List<CommentVo> list = new LinkedList<CommentVo>();
		List<CommentSchema> commentSchemaList = this.commentMapper.getPageOffSetList(articleId, referId, pageSize);
		for (CommentSchema item : commentSchemaList) {
			CommentVo comment = new CommentVo();
			
			comment.setId(item.getId());
			comment.setUuid(item.getUuid());
			comment.setContent(item.getContent());
			comment.setArticleId(item.getArticleId());
			comment.setCreateTime(item.getCreateTime());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			comment.setCreateTimeText(df.format(item.getCreateTime()));
			comment.setWriterUsername(item.getWriterUsername());

			list.add(comment);
		}
		page.setReferId(articleId);
		page.setOffSet(pageSize);
		page.setDatas(list);
		
		Boolean hasMoreRow = false;
		if(page.getDatas().size() < pageSize){
			hasMoreRow = false;
		} else if(page.getDatas().size() == pageSize){
			hasMoreRow = this.commentMapper.hasNextRow(articleId, commentSchemaList.get(commentSchemaList.size()-1).getId());
		}

		if(list.size() > 0){
			page.setMaxId(list.get(0).getId());
			page.setMinId(list.get(list.size()-1).getId());
		}
		page.setHasMoreRow(hasMoreRow);
		return page;
	}

}
